package cn.bdqn.controller;

import cn.bdqn.pojo.Employees;
import cn.bdqn.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author 小涛
 * @date: 2020/7/2 15:30
 */
@Controller
public class LoginController {

    @Autowired
    EmployeesService employeesService;

    /**
     * 访问/，跳转到登录页面
     * @return
     */
    @GetMapping("/")
    public String hello(){
        return "redirect:/login";
    }

    @GetMapping("/login")
    public String login(){

        return "login";
    }
    @RequestMapping("/weixinlogin")
    public String weixinLogin(HttpSession session, Model model, HttpServletRequest request, String code){
        String cd = request.getParameter("code");
        Employees employees = employeesService.findByOpenid(code);
        //下面我就简单输出了，获取了user，可以根据需求自己进行相应操作了
        if (employees == null){
            System.out.println("登录失败");
            model.addAttribute("msg","当前登录微信不匹配！");
            return "login";
        }else {
            System.out.println("登录成功");
            session.setAttribute("name",employees.getName());
            //用户Id
            session.setAttribute("id",employees.getId());

            //获得用户Id
            Integer perId= (Integer) session.getAttribute("id");

            //前台获得用户名称
            model.addAttribute("name",session.getAttribute("name"));


            System.out.println(employees.getName()+"输出name");
            return "index.html";
        }
    }
    @GetMapping("/loginTo")
    public String login(HttpSession session, Employees employees, Model model){
        //System.out.println("asdasdcsdf");
        Integer id =employeesService.login(employees);
//        System.out.println(id+"输出id");
//        String pwd=employeesService.password(employees);
//        System.out.println(pwd);

        //整个用户表的所有字段


//        session.setAttribute("ey","employees");
//        String name = session.get

        if (id == null ){
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
        else {
            employees= employeesService.getById(id);
//            System.out.println(employees);
//            System.out.println(employees.getName());
//            System.out.println(employees.getPassword());

            //用户名字
            session.setAttribute("name",employees.getName());
            //用户Id
            session.setAttribute("id",employees.getId());

            //获得用户Id
            Integer perId= (Integer) session.getAttribute("id");

            //前台获得用户名称
            model.addAttribute("name",session.getAttribute("name"));


//            System.out.println(employees.getName()+"输出name");
            return "index.html";
        }

    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }
}
